package com.tingyuge.base.jdk.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

/**
 * @Created by xiaodao
 */
public class AioServerHandler implements Runnable{
    public CountDownLatch latch;
    public AsynchronousServerSocketChannel channel;

    public AioServerHandler(int port) {
        //创建一个服务器
            latch = new CountDownLatch(1);
        try {
            channel = AsynchronousServerSocketChannel.open();
            channel.bind(new InetSocketAddress(port));
            System.out.println("服务端已经启动:  端口  "+port);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        //用于接收客户端的连接->异步
        channel.accept(this,new AcceptHandler());
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
